package controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.mybeans.form.FormBeanFactory;

import bean.Customer;
import bean.Fund;
import bean.FundTransaction;
import bean.LoggedUser;
import bean.Model;
import bean.TransactionStatus;
import bean.TransactionType;
import bean.UserType;
import dao.FundDAO;
import dao.FundTransactionDAO;
import dao.PositionDAO;
import formBean.SellFundForm;

/*
 * 
 * Created By Sahil Thakkar for Sell Fund Usecase
 */

public class SellFundAction extends Action {
	
	private FundTransactionDAO fundTransactionDAO;
	private FundTransaction fundTransaction;
	private FundDAO fundDAO;
	private PositionDAO positionDAO;
	private FormBeanFactory<SellFundForm> formBeanFactory = FormBeanFactory.getInstance(SellFundForm.class);
	
	public SellFundAction(Model model) {
		fundDAO = model.getFundDAO();
		positionDAO = model.getPositionDAO();
		this.fundTransactionDAO = model.getFundTransactionDAO();
	}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
	
	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "sellFund.do";
	}

	@Override
	public String perform(HttpServletRequest request) {
		// TODO Auto-generated method stub
		List<String> errors = new ArrayList<String>();
		request.setAttribute("errors", errors);
		String[] fundName;
		String[] fundSymbol;
		String[] fundPosition;
		String[] sellQnty;
		String soldFunds = "";
		try {
			
			HttpSession session     = request.getSession(false);
			LoggedUser user = (LoggedUser) session.getAttribute("user");
			
			SellFundForm form = formBeanFactory.create(request);
			
			fundPosition = positionDAO.getPositionByCusIdByFundId(""+user.getId());
			form.setFundPosition(fundPosition);
			request.setAttribute("form", form);
			
			if (!form.isPresent()) {
				return "openSellFund.do";
			}
			
			errors.addAll(form.getValidationErrors());
			if (errors.size() > 0) {
				return "openSellFund.do";
			}

			fundSymbol = form.getFundSymbol();
			fundName = form.getFundName();
			fundPosition = form.getFundPosition();
			sellQnty = form.getSellQnty();
			for(int i = 0; i <fundName.length; i++)
			{
				if(sellQnty[i].length() >0 && fundPosition[i].length()>0)
				{
					Double d1 = Double.parseDouble(sellQnty[i]);
					d1 = d1 * 1000;
					Double d2 = Double.parseDouble(fundPosition[i]);
					d2 = d2 * 1000;
					
					if(d2 < d1)
					{
						errors.add("Your Sell Quantity cannot be more than the number of Funds you hold.");
					}
				}
			}
			
			if (errors.size() > 0) {
				return "openSellFund.do";
			}
			
			for(int i = 0; i <fundName.length; i++)
			{
				if(sellQnty[i] != null && sellQnty[i] != "")
				{
					fundTransaction = new FundTransaction();
					Customer c = new Customer();
					c.setId(user.getId());
					fundTransaction.setCustomer(c);
					
					Fund f = fundDAO.getFundBySymbol(fundSymbol[i]);
					fundTransaction.setFund(f);
					Double d = Double.parseDouble(sellQnty[i]);
					d = d * 1000;
					long x = (long) Math.round(d);
					fundTransaction.setShares(x);
					fundTransaction.setTransactionType(TransactionType.SELL);
					fundTransaction.setOrderDate(new Date());
					fundTransaction.setStatus(TransactionStatus.Pending);
					fundTransactionDAO.createFundTransaction(fundTransaction);
					soldFunds = soldFunds + fundName[i] + " ";
				}
				
				
			}
			request.setAttribute("message", "You have sold the Fund."+soldFunds);
			return "employeeConsole.jsp";
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error.jsp";
		}
	}

	@Override
	public UserType getType() {
		// TODO Auto-generated method stub
		return UserType.Customer;
	}
}
